/**
 * 
 */
package com.firebird.fb.filter;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author qiuxj 创建于 2009-5-12
 *
 */
public class AuthFilter implements Filter {
	private final static Log log = LogFactory.getLog(AuthFilter.class);

	public static String LOGIN_AREA = "login_area";

	private String loginUrl = "/j_security_check";
	private Properties dpp = new Properties();
	private Map<String, String> dpMap = new HashMap<String, String>();

	public String getLoginUrl() {
		return loginUrl;
	}

	public void setLoginUrl(String loginUrl) {
		this.loginUrl = loginUrl;
	}

	/**
	 * @see javax.servlet.Filter#destroy()
	 */
	public void destroy() {
	}

	/**
	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
	 */
	public void init(FilterConfig filterConfig) throws ServletException {
	}

	public void initDisabledPageProperty() {
		/*try {
			dpp.loadFromXML(AuthFilter.class.getResourceAsStream("/disabledpages.xml"));

			for (int i = 0; i < Integer.valueOf(dpp.getProperty("totalCount")); i++) {
				dpMap.put(dpp.getProperty("disabledPage" + i), "true");
			}
		} catch (Exception e) {
			dpp.setProperty("isDisabled", "false");
			e.printStackTrace();
		}*/
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
			ServletException {

		if (!(request instanceof HttpServletRequest)) {
			throw new ServletException("Can only process HttpServletRequest");
		}

		if (!(response instanceof HttpServletResponse)) {
			throw new ServletException("Can only process HttpServletResponse");
		}

		/*if (dpp.getProperty("isDisabled").equals("true")) {
			String ctx = ((HttpServletRequest) request).getContextPath();
			String uri = ((HttpServletRequest) request).getRequestURI();

			int pathParamIndex = uri.indexOf(';');

			if (pathParamIndex > 0) {
				uri = uri.substring(0, pathParamIndex);
			}

			int queryParamIndex = uri.indexOf('?');

			if (queryParamIndex > 0) {
				uri = uri.substring(0, queryParamIndex);
			}

			if (dpMap.get(uri.replace(ctx, "")) != null) {
				((HttpServletResponse) response).sendRedirect(ctx + dpp.getProperty("turnToPage"));
				return;
			}
		}*/

		doFilterHttp((HttpServletRequest) request, (HttpServletResponse) response, chain);
	}

	protected void doFilterHttp(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
			throws IOException, ServletException {

		if (urlMatch(request, response, loginUrl)) {
			
		}

		chain.doFilter(request, response);
	}

	protected boolean urlMatch(HttpServletRequest request, HttpServletResponse response, String url) {
		String uri = request.getRequestURI();

		int pathParamIndex = uri.indexOf(';');

		if (pathParamIndex > 0) {
			uri = uri.substring(0, pathParamIndex);
		}

		int queryParamIndex = uri.indexOf('?');

		if (queryParamIndex > 0) {
			uri = uri.substring(0, queryParamIndex);
		}

		if ("".equals(request.getContextPath())) {
			return uri.endsWith(url);
		}

		return uri.endsWith(request.getContextPath() + url);
	}
}
